package dS.ProjectGPS.LeicaOWI;

import javax.microedition.lcdui.*;

public class FormOwiLOG extends Form implements FormOwi {

  OWIAgent owiAgent = null;

  ChoiceGroup device = new ChoiceGroup("Device", ChoiceGroup.EXCLUSIVE);
  TextField logFileSegments = new TextField("Log File Segments (sec)","", 4, TextField.NUMERIC);
  TextField jobName = new TextField("Job Name", "", 50, TextField.ANY);
  TextField filename = new TextField("Data Filename Prefix", "", 50,
                                     TextField.ANY);
  TextField antHeight = new TextField("Antenna Height (m)", "", 50,
                                      TextField.DECIMAL);
  ChoiceGroup antType = new ChoiceGroup("Antenna Type", ChoiceGroup.EXCLUSIVE);
  ChoiceGroup rateStatic = new ChoiceGroup("Rate Whilst Static",
                                           ChoiceGroup.POPUP);
  ChoiceGroup rateRoving = new ChoiceGroup("Rate Whilst Roving",
                                           ChoiceGroup.POPUP);
  ChoiceGroup posLoggingRoving = new ChoiceGroup(
     "Position Logging Rate Whilst Roving", ChoiceGroup.POPUP);

  public FormOwiLOG(OWIAgent o) {
    super("Logging");

    owiAgent = o;

    try {
      ndsInit();
    }
    catch (Exception ex) {
    }
  }

  public void ndsInit() throws Exception {

    append(rateStatic);
    append(rateRoving);
    append(posLoggingRoving);
    append(device);
    device.append("Sensor Internal", null);
    device.append("Sensor PC-Card", null);
    append(logFileSegments);
    append(jobName);
    append(filename);
    append(antHeight);
    append(antType);
    antType.append("Vertical", null);
    antType.append("Slope", null);
    rateStatic.append("Off", null);
    rateStatic.append("0.1 sec (10Hz)", null);
    rateStatic.append("0.2 sec (10Hz)", null);
    rateStatic.append("0.5 sec (10Hz)", null);
    rateStatic.append("1 sec", null);
    rateStatic.append("2 sec", null);
    rateStatic.append("3 sec", null);
    rateStatic.append("4 sec", null);
    rateStatic.append("5 sec", null);
    rateStatic.append("6 sec", null);
    rateStatic.append("10 sec", null);
    rateStatic.append("12 sec", null);
    rateStatic.append("15 sec", null);
    rateStatic.append("20 sec", null);
    rateStatic.append("30 sec", null);
    rateStatic.append("1 min", null);
    rateRoving.append("Off", null);
    rateRoving.append("0.1 sec (10Hz)", null);
    rateRoving.append("0.2 sec (10Hz)", null);
    rateRoving.append("0.5 sec (10Hz)", null);
    rateRoving.append("1 sec", null);
    rateRoving.append("2 sec", null);
    rateRoving.append("3 sec", null);
    rateRoving.append("4 sec", null);
    rateRoving.append("5 sec", null);
    rateRoving.append("6 sec", null);
    rateRoving.append("10 sec", null);
    rateRoving.append("12 sec", null);
    rateRoving.append("15 sec", null);
    rateRoving.append("20 sec", null);
    rateRoving.append("30 sec", null);
    rateRoving.append("1 min", null);
    posLoggingRoving.append("Off", null);
    posLoggingRoving.append("0.1 sec (10Hz)", null);
    posLoggingRoving.append("0.2 sec (10Hz)", null);
    posLoggingRoving.append("0.5 sec (10Hz)", null);
    posLoggingRoving.append("1 sec", null);
    posLoggingRoving.append("2 sec", null);
    posLoggingRoving.append("3 sec", null);
    posLoggingRoving.append("4 sec", null);
    posLoggingRoving.append("5 sec", null);
    posLoggingRoving.append("6 sec", null);
    posLoggingRoving.append("10 sec", null);
    posLoggingRoving.append("12 sec", null);
    posLoggingRoving.append("15 sec", null);
    posLoggingRoving.append("20 sec", null);
    posLoggingRoving.append("30 sec", null);
    posLoggingRoving.append("1 min", null);
  }

  public void update() {
    owiAgent.query(OWI.qsLOG());

    rateStatic.setSelectedIndex(OWI.logRateStatic, true);
    rateRoving.setSelectedIndex(OWI.logRateRoving, true);
    posLoggingRoving.setSelectedIndex(OWI.logPositionLoggingRate, true);
    device.setSelectedIndex(OWI.logDevice, true);
    logFileSegments.setString(OWI.logFileSegments);
    jobName.setString(OWI.logJobName);
    filename.setString(OWI.logFilenamePrefix);
    antHeight.setString(OWI.logAntennaHeight);
    antType.setSelectedIndex(OWI.logAntennaType, true);
  }

  public void save() {

    String msgId = "LOG";

    // Setup SET parameters
    String[] outputs = {
                       msgId,
                       "" + rateStatic.getSelectedIndex(),
                       "" + rateRoving.getSelectedIndex(),
                       "" + posLoggingRoving.getSelectedIndex(),
                       "" + device.getSelectedIndex(),
                       logFileSegments.getString(),
                       jobName.getString(),
                       filename.getString(),
                       antHeight.getString(),
                       "" + antType.getSelectedIndex()
    };

    //Send message and update form if successful
    int ackCode = (owiAgent.set(msgId, OWI.setMessage(outputs)));
    Controller.showACKAlert(this, this, ackCode);
  }

}
